<?php
namespace Mouta\AmistateBundle\Extension;

use Mouta\AmistateBundle\utils\Strings;
use Mouta\AmistateBundle\utils\Urls;
use Symfony\Bundle\FrameworkBundle\Routing\Router;

class LinksExtension extends \Twig_Extension {
    protected $router;
    
    public function  __construct(Router $router)
    {
        $this->router = $router;
        return $this->router;
    }
    
    public function getFilters() {
        return array(
            'var_dump'   => new \Twig_Filter_Function('var_dump'),
            'obfuscate'   => new \Twig_Filter_Method($this, 'obfuscate'),
            'fecha_escrita'   => new \Twig_Filter_Method($this, 'fecha_escrita'),
            'seo_url'  => new \Twig_Filter_Method($this, 'seo_url'),
            'build_tags'  => new \Twig_Filter_Method($this, 'build_tags'),
            'truncate'  => new \Twig_Filter_Method($this, 'truncate'),
            'concat_tag_sexo'  => new \Twig_Filter_Method($this, 'concat_tag_sexo'),
            'concat_tag_pais'  => new \Twig_Filter_Method($this, 'concat_tag_pais'),
            'concat_tag_para'  => new \Twig_Filter_Method($this, 'concat_tag_para'),
            'getLinkByTags'  => new \Twig_Filter_Method($this, 'getLinkByTags'),
            'getFbAppId'  => new \Twig_Filter_Method($this, 'getFbAppId'),
        );
    }
    
    public function getFbAppId(){
        return \Mouta\AmistateBundle\utils\SfFacebook::APPID;
    }

    public function seo_url($string) {
        return Strings::getSeoUrl($string);
    }
    public function obfuscate($string) {
        if(strlen($string)<3)
            return $string;
        return '*****'. substr($string,strlen($string)/2);
    }
    public function fecha_escrita($fecha) {
        $month = array("","Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiempre", "Octubre", "Noviembre", "Diciembre");
        $day = array("","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado","Domingo");
        return $day[$fecha->format("N")] . " " . $fecha->format("d") . " de " . $month[$fecha->format("n")] . ", ". $fecha->format("H:i") ." hs";
    }
    
    public function getLinkByTags($tags) {
        $tags = Urls::getTagsOrdenados($tags);
        if(count($tags)==0)
            return $this->router->generate('_homepage');
        if(count($tags)==1)
            return $this->router->generate('list1',array('tag1'=>$tags[0]->getTagGoogle()));
        if(count($tags)==2)
            return $this->router->generate('list2',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle()));
        if(count($tags)==3)
            return $this->router->generate('list3',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle(),'tag3'=>$tags[2]->getTagGoogle()));
    }
    
    public function concat_tag_sexo($string,$tag) {
        $tagSexo = new \Mouta\AmistateBundle\Entity\Tags();
        $tagSexo->setIdForm(18);
        $tagSexo->setTag($string);
        $tagSexo->setTagGoogle(Strings::getSeoUrl($string));
        $tag[] = $tagSexo;
        $tags = Urls::getTagsOrdenados($tag);
        if(count($tags)==1)
            return $this->router->generate('list1',array('tag1'=>$tags[0]->getTagGoogle()));
        if(count($tags)==2)
            return $this->router->generate('list2',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle()));
        if(count($tags)==3)
            return $this->router->generate('list3',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle(),'tag3'=>$tags[2]->getTagGoogle()));
    }
    
    public function concat_tag_pais($string,$tag) {
        $tagPais = new \Mouta\AmistateBundle\Entity\Tags();
        $tagPais->setIdForm(1);
        $tagPais->setTag($string);
        $tagPais->setTagGoogle(Strings::getSeoUrl($string));
        $tag[] = $tagPais;
        $tags = Urls::getTagsOrdenados($tag);
        if(count($tags)==1)
            return $this->router->generate('list1',array('tag1'=>$tags[0]->getTagGoogle()));
        if(count($tags)==2)
            return $this->router->generate('list2',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle()));
        if(count($tags)==3)
            return $this->router->generate('list3',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle(),'tag3'=>$tags[2]->getTagGoogle()));
    }
    
    public function concat_tag_para($string,$tag) {
        $tagPara = new \Mouta\AmistateBundle\Entity\Tags();
        $tagPara->setIdForm(19);
        $tagPara->setTag($string);
        $tagPara->setTagGoogle(Strings::getSeoUrl($string));
        $tag[] = $tagPara;
        $tags = Urls::getTagsOrdenados($tag);
        if(count($tags)==1)
            return $this->router->generate('list1',array('tag1'=>$tags[0]->getTagGoogle()));
        if(count($tags)==2)
            return $this->router->generate('list2',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle()));
        if(count($tags)==3)
            return $this->router->generate('list3',array('tag1'=>$tags[0]->getTagGoogle(),'tag2'=>$tags[1]->getTagGoogle(),'tag3'=>$tags[2]->getTagGoogle()));
    }
    
    public function truncate($string,$len) {
        $r = Strings::recortar($string,$len);
        if(strlen($string) != strlen($r))
            return $r.'...';
        else
            return $r;
    }
    
    public function build_tags($string,$user) {
        $tags = $user->getTags();
        $string = strip_tags($string);
        $tagsAux = array();
        $tagsEliminar = array();
        foreach($tags as $tag){
            $tagsAux[$tag->getTag()] = $tag;
            foreach ($tags  as $t2){
                if($tag->getTag() == $t2->getTag())
                    continue;
                $expresion = '/\b'.$t2->getTag().'\b/i';
                preg_match($expresion, $tag->getTag(), $coincidencias);
                //var_dump($coincidencias);
                if(count($coincidencias)>0)
                    $tagsEliminar[] = $t2->getTag();
            }
        }
        foreach ($tagsEliminar as $tag){
            unset($tagsAux[$tag]);
        }
        foreach($tagsAux as $tag){
            if($tag->getTagGoogle() == 'hombre' ||$tag->getTagGoogle() == 'mujer')
                continue;
            $expresion = '/\b'.$tag->getTag().'\b/i';
            $replace = $this->router->generate('list2',array('tag1'=> strtolower($user->getOpcionesSexo($user->getSexo())),'tag2'=>$tag->getTagGoogle()));
            $replace = '<a href="'.$replace.'">'.$tag->getTag().'</a>';
            $string = preg_replace($expresion, $replace, $string,1);
        }
        return $string;
    }

    public function getName()
    {
        return 'links_extension';
    }

}